* C:\Work\ClimChgWelfare\Models\Models_2015_04\DataToMatlab_Puma_150327.do
* Ryan Kellogg
* Created: Fri, 27 Mar 2015 (based on _150322 version created 22 Mar 2015)
* Last Modified: 


/*
This file loads in DataPUMA\ClimateRegressionData_150327.dta, which holds all regression-ready data.
The file sets up a matrix of q, binned temperatures, and controls to export to Matlab for running a local linear regression
*/

clear all
set more off 
capture log close
* cd M:\Private\SRA\Models_2010_06
* cd R:\Project\Wolff\Research\ClimateChangeMichigan\DropboxJuly112011\Models_2011_04\Inputs_Puma
cd C:\Work\ClimChgWelfare
use DataPUMA\ClimateRegressionData_150327.dta
* and jump back to Ryans cd.. 
cd Models\Models_2015_04\Inputs_PUMA


**************************************
* SOME FINAL SETUP
*******************************************
* Fix formatting of QOL variables so that all decimals are included in output files
format QOL* Price* Wage* age* sch* min* %9.0g

* Get inverse distances to coast, and powers thereof
replace mean_sea = 1 if mean_sea<1
replace mean_lake = 1 if mean_lake<1
gen SeaDistInv = 1 / mean_sea
gen SeaDistInv2 = SeaDistInv^2
gen SeaDistInv3 = SeaDistInv^3
gen LakeDistInv = 1 / mean_lake
gen LakeDistInv2 = LakeDistInv^2 
gen LakeDistInv3 = LakeDistInv^3

* Squared slope
rename slope_pct Slope
gen Slope2 = Slope^2


* Demographic controls
gen LogWeightedDensity = log(PopDens_Weighted)
gen LogDensity = log(PopDens)
gen Pct_HSEd = 1 - sch_hsdo
gen Pct_BSEd = sch_cdeg
gen Pct_GradEd = sch_post
sum Pct*
gen Age = age
drop age
gen PctHisp = min_hisp
gen PctBlack = min_blac
drop min_*


* Precipitation
egen PrecipAvg = rowmean(Precip1-Precip12)
egen PrecipSummer = rowmean(Precip4-Precip9)
egen PrecipWinter = rowmean(Precip10-Precip12 Precip1-Precip3)
drop Precip1-Precip12

* Seasonal dew points
egen DewPtAvg = rowmean(DewPt1-DewPt12)
egen DewPtSummer = rowmean(DewPt4-DewPt9)
egen DewPtWinter = rowmean(DewPt10-DewPt12 DewPt1-DewPt3)
egen DewPtSummerTight = rowmean(DewPt6-DewPt8)
drop DewPt1-DewPt12

* Seasonal rel humidity
egen RelHumAvg = rowmean(RelHum1-RelHum12)
egen RelHumSummer = rowmean(RelHum4-RelHum9)
egen RelHumWinter = rowmean(RelHum10-RelHum12 RelHum1-RelHum3)
egen RelHumSummerTight = rowmean(RelHum6-RelHum8)
drop RelHum1-RelHum12

* Seasonal sunshine (interpolated using all weather stations)
egen SunAvg = rowmean(Sun1-Sun12)
egen SunSummer = rowmean(Sun4-Sun9)
egen SunWinter = rowmean(Sun10-Sun12 Sun1-Sun3)
drop Sun1-Sun12

* Seasonal Sunshine with "4S" data (interpolated from four closest weather stations of PUMAS centroid). 
egen Sun4SAvg = rowmean(Sun4S1-Sun4S12)
egen Sun4SSummer = rowmean(Sun4S4-Sun4S9)
egen Sun4SWinter = rowmean(Sun4S10-Sun4S12 Sun4S1-Sun4S3)
drop Sun4S1-Sun4S12

* Seasonal change in precipitation
egen DeltaA2PrecipAvg = rowmean(DeltaA2Ensemble30YearPrecipT1-DeltaA2Ensemble30YearPrecipT12)
egen DeltaA2PrecipSummer = rowmean(DeltaA2Ensemble30YearPrecipT4-DeltaA2Ensemble30YearPrecipT9)
egen DeltaA2PrecipWinter = rowmean(DeltaA2Ensemble30YearPrecipT10-DeltaA2Ensemble30YearPrecipT12 DeltaA2Ensemble30YearPrecipT1-DeltaA2Ensemble30YearPrecipT3)
drop DeltaA2Ensemble30YearPrecipT1-DeltaA2Ensemble30YearPrecipT12
egen DeltaA1F1PrecipAvg = rowmean(DeltaA1F130YearPrecipT1-DeltaA1F130YearPrecipT12)
egen DeltaA1F1PrecipSummer = rowmean(DeltaA1F130YearPrecipT4-DeltaA1F130YearPrecipT9)
egen DeltaA1F1PrecipWinter = rowmean(DeltaA1F130YearPrecipT10-DeltaA1F130YearPrecipT12 DeltaA1F130YearPrecipT1-DeltaA1F130YearPrecipT3)
drop DeltaA1F130YearPrecipT1-DeltaA1F130YearPrecipT12

* Seasonal change in dew point
drop DeltaA2Ensemble30YearTempAvg* DeltaA1F130YearTempAvg*
order DeltaA2Ensemble30YearDewPt*
egen DeltaA2DewPtAvg = rowmean(DeltaA2Ensemble30YearDewPt1-DeltaA2Ensemble30YearDewPt12)
egen DeltaA2DewPtSummer = rowmean(DeltaA2Ensemble30YearDewPt4-DeltaA2Ensemble30YearDewPt9)
egen DeltaA2DewPtWinter = rowmean(DeltaA2Ensemble30YearDewPt10-DeltaA2Ensemble30YearDewPt12 DeltaA2Ensemble30YearDewPt1-DeltaA2Ensemble30YearDewPt3)
egen DeltaA2DewPtSummerTight = rowmean(DeltaA2Ensemble30YearDewPt6-DeltaA2Ensemble30YearDewPt8)
drop DeltaA2Ensemble30YearDewPt*
order DeltaA1F130YearDewPt*
egen DeltaA1F1DewPtAvg = rowmean(DeltaA1F130YearDewPt1-DeltaA1F130YearDewPt12)
egen DeltaA1F1DewPtSummer = rowmean(DeltaA1F130YearDewPt4-DeltaA1F130YearDewPt9)
egen DeltaA1F1DewPtWinter = rowmean(DeltaA1F130YearDewPt10-DeltaA1F130YearDewPt12 DeltaA1F130YearDewPt1-DeltaA1F130YearDewPt3)
egen DeltaA1F1DewPtSummerTight = rowmean(DeltaA1F130YearDewPt6-DeltaA1F130YearDewPt8)
drop DeltaA1F130YearDewPt*

* Seasonal change in rel humidity
egen DeltaA2RelHumAvg = rowmean(DeltaA2Ensemble30YearRelHum1-DeltaA2Ensemble30YearRelHum12)
egen DeltaA2RelHumSummer = rowmean(DeltaA2Ensemble30YearRelHum4-DeltaA2Ensemble30YearRelHum9)
egen DeltaA2RelHumWinter = rowmean(DeltaA2Ensemble30YearRelHum10-DeltaA2Ensemble30YearRelHum12 DeltaA2Ensemble30YearRelHum1-DeltaA2Ensemble30YearRelHum3)
egen DeltaA2RelHumSummerTight = rowmean(DeltaA2Ensemble30YearRelHum6-DeltaA2Ensemble30YearRelHum8)
drop DeltaA2Ensemble30YearRelHum*
egen DeltaA1F1RelHumAvg = rowmean(DeltaA1F130YearRelHum1-DeltaA1F130YearRelHum12)
egen DeltaA1F1RelHumSummer = rowmean(DeltaA1F130YearRelHum4-DeltaA1F130YearRelHum9)
egen DeltaA1F1RelHumWinter = rowmean(DeltaA1F130YearRelHum10-DeltaA1F130YearRelHum12 DeltaA1F130YearRelHum1-DeltaA1F130YearRelHum3)
egen DeltaA1F1RelHumSummerTight = rowmean(DeltaA1F130YearRelHum6-DeltaA1F130YearRelHum8)
drop DeltaA1F130YearRelHum*

* Seasonal change in cloud fraction
egen DeltaA2CloudFracAvg = rowmean(DeltaA2Ensemble30YearCloudFrac1-DeltaA2Ensemble30YearCloudFrac12)
egen DeltaA2CloudFracSummer = rowmean(DeltaA2Ensemble30YearCloudFrac4-DeltaA2Ensemble30YearCloudFrac9)
egen DeltaA2CloudFracWinter = rowmean(DeltaA2Ensemble30YearCloudFrac10-DeltaA2Ensemble30YearCloudFrac12 DeltaA2Ensemble30YearCloudFrac1-DeltaA2Ensemble30YearCloudFrac3)
drop DeltaA2Ensemble30YearCloudFrac*
egen DeltaA1F1CloudFracAvg = rowmean(DeltaA1F130YearCloudFrac1-DeltaA1F130YearCloudFrac12)
egen DeltaA1F1CloudFracSummer = rowmean(DeltaA1F130YearCloudFrac4-DeltaA1F130YearCloudFrac9)
egen DeltaA1F1CloudFracWinter = rowmean(DeltaA1F130YearCloudFrac10-DeltaA1F130YearCloudFrac12 DeltaA1F130YearCloudFrac1-DeltaA1F130YearCloudFrac3)
drop DeltaA1F130YearCloudFrac*


* Create census division indicator
gen Division = 0
replace Division = 11 if inlist(statefip,9,23,25,33,44,50)
replace Division = 12 if inlist(statefip,34,36,42)
replace Division = 23 if inlist(statefip,17,18,26,39,55)
replace Division = 24 if inlist(statefip,19,20,27,29,31,38,46)
replace Division = 35 if inlist(statefip,10,11,12,13,24,37,45,51,54)
replace Division = 36 if inlist(statefip,1,21,28,47)
replace Division = 37 if inlist(statefip,5,22,40,48)
replace Division = 48 if inlist(statefip,4,8,16,30,32,35,49,56)
replace Division = 49 if inlist(statefip,2,6,15,41,53)

order statefip PumaID msa msaname Division
sort statefip PumaID
save temp.dta, replace






/******START EXPORTING ITEMS TO MATLAB***********/
/******DO NOT RE-SORT THE DATA!******************/

* IDENTIFIERS AND DEPENDENT VARIABLES: Statefip, PUMAID, msa, QOL, P, W MEASURES
clear
use temp.dta
keep statefip PumaID msa QOL QOL_orig QOL_25_1 QOL_GM QOL_Dahl QOL_fulladj QOL_doubleadj QOL_noncol QOL_col Price Wage_orig Wage 
gen blah = statefip		/* to get the state fip, not the state name */
drop statefip
order blah PumaID msa QOL QOL_orig QOL_25_1 QOL_GM QOL_Dahl QOL_fulladj QOL_doubleadj QOL_noncol QOL_col Price Wage_orig Wage 
outsheet using QOL_ToMatlab.csv, comma replace nonames


* TEMPERATURE BINS (CLIMATE NORMALS)
clear
use temp.dta
keep bin*
outsheet using ClimateBins_ToMatlab.csv, comma replace nonames



* DELTA A2 2100 ENSEMBLE BINNED TEMPERATURES
clear
use temp.dta
keep DeltaA2Ensemble30Year_bin*
outsheet using DeltaA2Ensemble2100_ToMatlab.csv, comma replace nonames



* DELTA A1F1 2100 
clear
use temp.dta
keep DeltaA1F130Year_bin*
outsheet using DeltaA1F12100_ToMatlab.csv, comma replace nonames


* GEOGRAPHIC CONTROLS
clear
use temp.dta
keep Slope SeaDistInv LakeDistInv SeaDistInv2 LakeDistInv2 SeaDistInv3 LakeDistInv3 Slope2
order Slope SeaDistInv LakeDistInv SeaDistInv2 LakeDistInv2 SeaDistInv3 LakeDistInv3 Slope2
outsheet using Controls_Geographic_ToMatlab.csv, comma replace nonames



* OTHER WEATHER: PRECIPITATION, REL HUMIDITY, SUNSHINE (ANNUAL AVERAGES)
clear
use temp.dta
keep PrecipAvg RelHumAvg SunAvg
order PrecipAvg RelHumAvg SunAvg
outsheet using Controls_OtherWx_RH_ToMatlab.csv, comma replace nonames

* OTHER WEATHER: PRECIPITATION, DEW POINT, SUNSHINE (ANNUAL AVERAGES)
clear
use temp.dta
keep PrecipAvg DewPtAvg SunAvg
order PrecipAvg DewPtAvg SunAvg
outsheet using Controls_OtherWx_DP_ToMatlab.csv, comma replace nonames



* OTHER WEATHER 2: PRECIPITATION, SUMMER REL HUMIDITY, SUNSHINE
clear
use temp.dta
keep PrecipAvg RelHumSummerTight SunAvg
order PrecipAvg RelHumSummerTight SunAvg
outsheet using Controls_OtherWx2_RH_ToMatlab.csv, comma replace nonames

* OTHER WEATHER 2: PRECIPITATION, SUMMER DEW POINT, SUNSHINE
clear
use temp.dta
keep PrecipAvg DewPtSummerTight SunAvg
order PrecipAvg DewPtSummerTight SunAvg
outsheet using Controls_OtherWx2_DP_ToMatlab.csv, comma replace nonames



* OTHER WEATHER 3: SUMMER & WINTER PRECIPITATION, SUMMER & WINTER REL HUMIDITY, SUMMER & WINTER SUNSHINE
clear
use temp.dta
keep PrecipSummer PrecipWinter RelHumSummer RelHumWinter SunSummer SunWinter
order PrecipSummer PrecipWinter RelHumSummer RelHumWinter SunSummer SunWinter
outsheet using Controls_OtherWx3_RH_ToMatlab.csv, comma replace nonames

* OTHER WEATHER 3: SUMMER & WINTER PRECIPITATION, SUMMER & WINTER DEW POINT, SUMMER & WINTER SUNSHINE
clear
use temp.dta
keep PrecipSummer PrecipWinter DewPtSummer DewPtWinter SunSummer SunWinter
order PrecipSummer PrecipWinter DewPtSummer DewPtWinter SunSummer SunWinter
outsheet using Controls_OtherWx3_DP_ToMatlab.csv, comma replace nonames


* REPEAT OTHER WEATHER, OTHER WEATHER 2, and OTHER WEATHER 3 for the new Sunshine 4S data, 

* OTHER WEATHER (4S): PRECIPITATION, REL HUMIDITY, SUNSHINE4S (ANNUAL AVERAGES)
clear
use temp.dta
keep PrecipAvg RelHumAvg Sun4SAvg
order PrecipAvg RelHumAvg Sun4SAvg
outsheet using Controls_OtherWx4S_RH_ToMatlab.csv, comma replace nonames

* OTHER WEATHER (4S): PRECIPITATION, DEW POINT, SUNSHINE4S (ANNUAL AVERAGES)
clear
use temp.dta
keep PrecipAvg DewPtAvg Sun4SAvg
order PrecipAvg DewPtAvg Sun4SAvg
outsheet using Controls_OtherWx4S_DP_ToMatlab.csv, comma replace nonames



* OTHER WEATHER (4S) 2: PRECIPITATION, SUMMER REL HUMIDITY, SUNSHINE4S
clear
use temp.dta
keep PrecipAvg RelHumSummerTight Sun4SAvg
order PrecipAvg RelHumSummerTight Sun4SAvg
outsheet using Controls_OtherWx4S2_RH_ToMatlab.csv, comma replace nonames

* OTHER WEATHER (4S) 2: PRECIPITATION, SUMMER DEW POINT, SUNSHINE4S
clear
use temp.dta
keep PrecipAvg DewPtSummerTight Sun4SAvg
order PrecipAvg DewPtSummerTight Sun4SAvg
outsheet using Controls_OtherWx4S2_DP_ToMatlab.csv, comma replace nonames


* OTHER WEATHER (4S) 3: SUMMER & WINTER PRECIPITATION, SUMMER & WINTER REL HUMIDITY, SUMMER & WINTER SUNSHINE4S
clear
use temp.dta
keep PrecipSummer PrecipWinter RelHumSummer RelHumWinter Sun4SSummer Sun4SWinter
order PrecipSummer PrecipWinter RelHumSummer RelHumWinter Sun4SSummer Sun4SWinter
outsheet using Controls_OtherWx4S3_RH_ToMatlab.csv, comma replace nonames

* OTHER WEATHER (4S) 3: SUMMER & WINTER PRECIPITATION, SUMMER & WINTER DEW POINT, SUMMER & WINTER SUNSHINE4S
clear
use temp.dta
keep PrecipSummer PrecipWinter DewPtSummer DewPtWinter Sun4SSummer Sun4SWinter
order PrecipSummer PrecipWinter DewPtSummer DewPtWinter Sun4SSummer Sun4SWinter
outsheet using Controls_OtherWx4S3_DP_ToMatlab.csv, comma replace nonames
 

* DEMOGRAPHIC / HUMAN CONTROLS
clear
use temp.dta
keep LogWeightedDensity Pct* Age
order LogWeightedDensity Pct_HSEd Pct_BSEd Pct_GradEd Age PctHisp PctBlack
outsheet using Controls_Demog_ToMatlab.csv, comma replace nonames


* UNWEIGHTED POPULATION DENSITY
clear
use temp.dta
keep LogDensity
outsheet using LogPopDens_ToMatlab.csv, comma replace nonames

* CLIMATE CHANGE PROJECTIONS FOR OTHER WEATHER VARIABLES, REL HUM
clear
use temp.dta
keep DeltaA2PrecipAvg DeltaA2RelHumAvg DeltaA2CloudFracAvg
order DeltaA2PrecipAvg DeltaA2RelHumAvg DeltaA2CloudFracAvg
outsheet using DeltaA2Ensemble_OtherWx_RH_2100_ToMatlab.csv, comma replace nonames
clear
use temp.dta
keep DeltaA1F1PrecipAvg DeltaA1F1RelHumAvg DeltaA1F1CloudFracAvg
order DeltaA1F1PrecipAvg DeltaA1F1RelHumAvg DeltaA1F1CloudFracAvg
outsheet using DeltaA1F1_OtherWx_RH_2100_ToMatlab.csv, comma replace nonames

* CLIMATE CHANGE PROJECTIONS FOR OTHER WEATHER VARIABLES, DEW PT
clear
use temp.dta
keep DeltaA2PrecipAvg DeltaA2DewPtAvg DeltaA2CloudFracAvg
order DeltaA2PrecipAvg DeltaA2DewPtAvg DeltaA2CloudFracAvg
outsheet using DeltaA2Ensemble_OtherWx_DP_2100_ToMatlab.csv, comma replace nonames
clear
use temp.dta
keep DeltaA1F1PrecipAvg DeltaA1F1DewPtAvg DeltaA1F1CloudFracAvg
order DeltaA1F1PrecipAvg DeltaA1F1DewPtAvg DeltaA1F1CloudFracAvg
outsheet using DeltaA1F1_OtherWx_DP_2100_ToMatlab.csv, comma replace nonames


* CLIMATE CHANGE PROJECTIONS FOR OTHER WEATHER 2 VARIABLES, REL HUM
clear
use temp.dta
keep DeltaA2PrecipAvg DeltaA2RelHumSummerTight DeltaA2CloudFracAvg
order DeltaA2PrecipAvg DeltaA2RelHumSummerTight DeltaA2CloudFracAvg
outsheet using DeltaA2Ensemble_OtherWx2_RH_2100_ToMatlab.csv, comma replace nonames
clear
use temp.dta
keep DeltaA1F1PrecipAvg DeltaA1F1RelHumSummerTight DeltaA1F1CloudFracAvg
order DeltaA1F1PrecipAvg DeltaA1F1RelHumSummerTight DeltaA1F1CloudFracAvg
outsheet using DeltaA1F1_OtherWx2_RH_2100_ToMatlab.csv, comma replace nonames

* CLIMATE CHANGE PROJECTIONS FOR OTHER WEATHER 2 VARIABLES, DEW PT
clear
use temp.dta
keep DeltaA2PrecipAvg DeltaA2DewPtSummerTight DeltaA2CloudFracAvg
order DeltaA2PrecipAvg DeltaA2DewPtSummerTight DeltaA2CloudFracAvg
outsheet using DeltaA2Ensemble_OtherWx2_DP_2100_ToMatlab.csv, comma replace nonames
clear
use temp.dta
keep DeltaA1F1PrecipAvg DeltaA1F1DewPtSummerTight DeltaA1F1CloudFracAvg
order DeltaA1F1PrecipAvg DeltaA1F1DewPtSummerTight DeltaA1F1CloudFracAvg
outsheet using DeltaA1F1_OtherWx2_DP_2100_ToMatlab.csv, comma replace nonames


* CLIMATE CHANGE PROJECTIONS FOR OTHER WEATHER 3 VARIABLES, REL HUM
clear
use temp.dta
keep DeltaA2PrecipSummer DeltaA2PrecipWinter DeltaA2RelHumSummer DeltaA2RelHumWinter DeltaA2CloudFracSummer DeltaA2CloudFracWinter
order DeltaA2PrecipSummer DeltaA2PrecipWinter DeltaA2RelHumSummer DeltaA2RelHumWinter DeltaA2CloudFracSummer DeltaA2CloudFracWinter
outsheet using DeltaA2Ensemble_OtherWx3_RH_2100_ToMatlab.csv, comma replace nonames
clear
use temp.dta
keep DeltaA1F1PrecipSummer DeltaA1F1PrecipWinter DeltaA1F1RelHumSummer DeltaA1F1RelHumWinter DeltaA1F1CloudFracSummer DeltaA1F1CloudFracWinter
order DeltaA1F1PrecipSummer DeltaA1F1PrecipWinter DeltaA1F1RelHumSummer DeltaA1F1RelHumWinter DeltaA1F1CloudFracSummer DeltaA1F1CloudFracWinter
outsheet using DeltaA1F1_OtherWx3_RH_2100_ToMatlab.csv, comma replace nonames

* CLIMATE CHANGE PROJECTIONS FOR OTHER WEATHER 3 VARIABLES, DEW PT
clear
use temp.dta
keep DeltaA2PrecipSummer DeltaA2PrecipWinter DeltaA2DewPtSummer DeltaA2DewPtWinter DeltaA2CloudFracSummer DeltaA2CloudFracWinter
order DeltaA2PrecipSummer DeltaA2PrecipWinter DeltaA2DewPtSummer DeltaA2DewPtWinter DeltaA2CloudFracSummer DeltaA2CloudFracWinter
outsheet using DeltaA2Ensemble_OtherWx3_DP_2100_ToMatlab.csv, comma replace nonames
clear
use temp.dta
keep DeltaA1F1PrecipSummer DeltaA1F1PrecipWinter DeltaA1F1DewPtSummer DeltaA1F1DewPtWinter DeltaA1F1CloudFracSummer DeltaA1F1CloudFracWinter
order DeltaA1F1PrecipSummer DeltaA1F1PrecipWinter DeltaA1F1DewPtSummer DeltaA1F1DewPtWinter DeltaA1F1CloudFracSummer DeltaA1F1CloudFracWinter
outsheet using DeltaA1F1_OtherWx3_DP_2100_ToMatlab.csv, comma replace nonames


* STATE FE
clear
use temp.dta
keep statefip
xi i.statefip
drop statefip
outsheet using StateFE_ToMatlab.csv, comma replace nonames



* DIVISION FE
clear
use temp.dta
keep Division
xi i.Division
drop Division
outsheet using DivisionFE_ToMatlab.csv, comma replace nonames



* BOOTSTRAP INDICATORS
clear
use temp.dta
egen BootstrapDivision = group(Division)
keep BootstrapMSA BootstrapState BootstrapDivision
outsheet using Bootstrap_ToMatlab.csv, comma replace nonames



* INCOME. NEEDED TO SUM WELFARE CHANGES OVER COUNTIES
clear
use temp.dta
keep Income
outsheet using Income_ToMatlab.csv, comma replace nonames



* POPULATION
clear
use temp.dta
keep Population Population_NonCol Population_Col
order Population Population_NonCol Population_Col
outsheet using Population_ToMatlab.csv, comma replace nonames
